package cn.daomeng.firebird.core.utils;

import java.util.concurrent.*;

/**
 * 线程池工具类
 *
 * @author Qin.SiLiang
 */
public class ExecutorUtils {

    /** 核心线程数，线程闲置的时候也不会收回，当线程数小于核心线程数时，即使有线程空闲，线程池也会优先创建新线程处理*/
    private static final int CORE_POOL_SIZE  = 50;
    /**最大线数，当线程数>=corePoolSize，且任务队列已满时。线程池会创建新线程来处理任务*/
    private static final int MAX_POOL_SIZE = 100;
    /**当线程空闲时间达到keepAliveTime时，线程会退出，直到线程数量=corePoolSize*/
    private static final Long KEEP_ALIVE_TIME = 0L;
    /** 当核心线程数达到最大时，新任务会放在队列中排队等待执行*/
    private static final int QUEUE_LENGTH = 30000;


    public static ThreadPoolExecutor SCHEDULED_THREAD_POOL = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(QUEUE_LENGTH),
            new ThreadFactory(){
                @Override
                public Thread newThread(Runnable r) {
                return new Thread(r, "cloud_thread_getToken_" + r.hashCode());
            }},
            new ThreadPoolExecutor.AbortPolicy());

    public static ExecutorService getThreadPool() {
        return SCHEDULED_THREAD_POOL;
    }

}
